/**
 * Copyright © 2014 Winit Corp. All rights reserved.
 * This software is proprietary to and embodies the confidential
 * technology of Winit Corp.  Possession, use, or copying
 * of this software and media is authorized only pursuant to a
 * valid written license from Winit Corp or an authorized sublicensor.
 */
package com.winit.openapi.tools.controller;

import java.util.Date;
import java.util.TimeZone;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.DateUtil;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.utils.ValidateDateUtitls;
import com.winit.ups.spi.command.CodeCommand;
import com.winit.ups.spi.warehouse.WarehouseService;
import com.winit.ups.spi.warehouse.vo.WarehouseVo;

/**
 * 时间转换公共接口
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * fei.li 	1.0  		2017年11月2日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/winit/tools/date")
public class WinitToolsDateConvertController extends BaseController {

    @Resource
    private WarehouseService upsWarehouseService;

    @RequestMapping(value = "/convert")
    public String DateConvert(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        // 声明返回参数Date
        Date resultDate = null;
        if (obj instanceof JSONObject) {
            JSONObject jsonObj = (JSONObject) obj;

            ValidateUtil.validNotNull(jsonObj, "localDate");
            // 原始时间
            ValidateDateUtitls.validDate(jsonObj.getString("localDate"), "localDate");
            Date localDate = DateUtil.parse2Date(jsonObj.getString("localDate").trim(), DateUtil.FULL_DATE_STRING);
            // 如果有目标时区传进来，直接根据目标时区去转换
            if (StringUtils.isNotBlank(jsonObj.getString("targetTimeZone"))) {
                // 如果是有传本地时区，就根据本地时区转
                if (StringUtils.isNotBlank(jsonObj.getString("localTimeZone"))) {
                    resultDate = DateUtil.convertDateOnTimeZone(localDate.getTime(),
                        TimeZone.getTimeZone(jsonObj.getString("localTimeZone")),
                        TimeZone.getTimeZone(jsonObj.getString("targetTimeZone")));
                } else { // 如果没有传本地时区，就根据默认时区转
                    resultDate = DateUtil.convertDateOnTimeZone(localDate.getTime(),
                        TimeZone.getDefault(),
                        TimeZone.getTimeZone(jsonObj.getString("targetTimeZone")));
                }
            } else if (StringUtils.isNotBlank(jsonObj.getString("warehouseCode"))) { // 如果没有目标时区，就根据仓库编码取查询，然后取出目标时区，然后再进行转换
                ValidateUtil.validNotNull(jsonObj, "warehouseCode");
                ValidateUtil.validMaxLength(jsonObj, "warehouseCode", 128);
                // 构建参数查询仓库信息
                CodeCommand command = new CodeCommand();
                command.setCode(jsonObj.getString("warehouseCode"));
                WarehouseVo warehouseVo = upsWarehouseService.queryWarehouseByCode(command);
                if (warehouseVo != null) {
                    // 如果是有传本地时区，就根据本地时区转
                    if (StringUtils.isNotBlank(jsonObj.getString("localTimeZone"))) {
                        resultDate = DateUtil.convertDateOnTimeZone(localDate.getTime(),
                            TimeZone.getTimeZone(jsonObj.getString("localTimeZone")),
                            TimeZone.getTimeZone(warehouseVo.getNewTimeZone()));
                    } else { // 如果没有传本地时区，就根据默认时区转
                        resultDate = DateUtil.convertDateOnTimeZone(localDate.getTime(),
                            TimeZone.getDefault(),
                            TimeZone.getTimeZone(warehouseVo.getNewTimeZone()));
                    }
                }
            } else { // 如果目标时区没有，仓库编码没有，则抛异常
                throw new ApiException(ErrorCode.FIELD_NOT_NULL, "data");
            }
            // 如果都没有，则抛异常，必须参数为空
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 返回
        responseMsg.setData(resultDate);
        return SUCCESS;
    }
}
